/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */
package com.happycolor.littlelove.base;

import android.util.Log;

/**
 * 
 * @author panguso
 * @date 2012-9-28
 */
public class HCLog {
	private static final int logLevel = Log.VERBOSE;
	private static HCLog mInstance = null;
	private static String tag = "Panguso";
	private boolean isDebug = true;

	/**
	 * 
	 * @param logTag ：
	 */
	public HCLog(String logTag) {
		tag = logTag;
	}

	/**
	 * 
	 * @param logTag :
	 * @return
	 * @author xieyongqiang
	 * @date Nov 23, 2012
	 */
	public static HCLog getInstance(String logTag) {
		if (mInstance == null) {
			synchronized (HCLog.class) {
				mInstance = new HCLog(logTag);
			}
		} else {
			tag = logTag;
		}
		return HCLog.mInstance;
	}

	/**
	 * 
	 * @param str :
	 */
	public void d(Object str) {
		if (this.isDebug) {
			this.debug(str);
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void debug(Object str) {
		if (HCLog.logLevel <= Log.DEBUG) {
			String name = this.getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.d(tag, ls);
		}
	}

	/**
	 * 
	 * @param ex :
	 */
	public void e(Exception ex) {
		if (this.isDebug) {
			this.error(ex);
		}
	}

	/**
	 * 
	 * @param str :
	 * @date 2012-10-11
	 */
	public void e(Object str) {
		if (this.isDebug) {
			this.error(str);
		}
	}

	/**
	 * 
	 * @param ex :
	 */
	public void error(Exception ex) {
		if (HCLog.logLevel <= Log.ERROR) {
			StringBuffer sb = new StringBuffer();
			String name = this.getFunctionName();

			StackTraceElement[] sts = ex.getStackTrace();

			if (name != null) {
				sb.append(name + " - " + ex + "\r\n");
			} else {
				sb.append(ex + "\r\n");
			}

			if (sts != null && sts.length > 0) {
				for (StackTraceElement st : sts) {
					if (st != null) {
						sb.append("[ " + st.getFileName() + ":" + st.getLineNumber() + " ]\r\n");
					}
				}
			}

			Log.e(tag, sb.toString());
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void error(Object str) {
		if (HCLog.logLevel <= Log.ERROR) {
			String name = this.getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.e(tag, ls);
		}
	}

	private String getFunctionName() {
		StackTraceElement[] sts = Thread.currentThread().getStackTrace();

		if (sts == null) {
			return null;
		}

		for (StackTraceElement st : sts) {
			if (st.isNativeMethod()) {
				continue;
			}

			if (st.getClassName().equals(Thread.class.getName())) {
				continue;
			}

			if (st.getClassName().equals(this.getClass().getName())) {
				continue;
			}

			return "[" + Thread.currentThread().getId() + ": " + st.getFileName() + ":"
			        + st.getLineNumber() + "]";
		}

		return null;
	}

	/**
	 * 
	 * @param str :
	 */
	public void i(Object str) {
		if (this.isDebug) {
			this.info(str);
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void info(Object str) {
		if (HCLog.logLevel <= Log.INFO) {
			String name = this.getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.i(tag, ls);
		}
	}

	public void setTag(String tag1) {
		tag = tag1;
	}

	/**
	 * 
	 * @param str :
	 */
	public void v(Object str) {
		if (this.isDebug) {
			this.verbose(str);
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void verbose(Object str) {
		if (HCLog.logLevel <= Log.VERBOSE) {
			String name = this.getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.v(tag, ls);
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void w(Object str) {
		if (this.isDebug) {
			this.warn(str);
		}
	}

	/**
	 * 
	 * @param str :
	 */
	public void warn(Object str) {
		if (HCLog.logLevel <= Log.WARN) {
			String name = this.getFunctionName();
			String ls = (name == null ? str.toString() : (name + " - " + str));
			Log.w(tag, ls);
		}
	}
}